<template>
  <div class="list-wrap" ref='wrapper'>
    <ul class="list-content">
      <li class="current-ctiy">
        <div class="title">当前城市</div>
        <div class="btn-list">
          <div class="btn-wrapper">
				{{this.currentCity}}
          </div>
        </div>
      </li>
      <li class="hot-city">
        <div class="title">热门城市</div>
        <div class="btn-list" >
          <div class="btn-wrapper"
          v-for="(list,index) in hotcities"
          :key="index"
          @click="cityClick(list)">
           {{list.name}}
          </div>
        </div>
      </li>
      <li class="alphabet">
        <div class="title">字母排序</div>
        <div class="alph-list">
         <div class="alph-wrapper"
         @click="letterChange(key)"
         :key="key"
         v-for="(item,key) in cities">
          {{key}}
         </div>
        </div>
      </li>
      <li class="city-list"
      :key="key"
      :ref='key'
        v-for="(item,key) in cities">
        <div class="title">{{key}}</div>
        <div class="item-list">
         <div class="item"  :key="index"
          @click="cityClick(list)"
         v-for="(list,index) in item">
              {{list.name}}
         </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'
  import {mapState,mapMutations} from 'vuex'
  export default{
    name:'CityList',
    props:['hotcities','cities'],
    data(){
      return {
        letter:''
      }
    },
    computed:{
      ...mapState({
        currentCity:'city'
      })
    },
    mounted (){
        this.scroll = new BScroll(this.$refs.wrapper,{
          click:true,
          tap:true
        });
    },
    methods:{
      ...mapMutations({
        changeCity:'changeCity'
      }),
      letterChange(val){
        this.letter = val;
      },
      cityClick(obj){
        //改变仓库的当前城市，mutaions中的函数
        this.changeCity(obj.name);
  
        this.$router.push({
          path:'/'
        })
      }
    },
    watch:{
        letter:function(){
          if(this.letter){
             //获取需要滚动的元素
             const letterEle = this.$refs[this.letter][0];
             this.scroll.scrollToElement(letterEle);
          }
        }
    }
  }
</script>

<style scoped="scoped">
  .list-wrap{
    overflow: hidden;
    background-color: #eee;
    position: absolute;
    left: 0;
    top: 1.75rem;
    right: 0;
    bottom: 0;
  }
  .title{
    height: 0.8rem;
    line-height: 0.8rem;
    padding-left: 0.2rem;
    background: #eee;
    border-top: 0.01rem solid #eee;
     border-bottom: 0.01rem solid #ccc;
  }
  .btn-list{
    overflow: hidden;
    padding:0.2rem;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
  }
  .btn-list .btn-wrapper{
    width: 28%;
    height: 0.6rem;
    text-align: center;
    line-height: 0.6rem;
    margin:0.15rem 0.1rem;
    border-radius: 0.6rem;
    border: 1px solid #ccc;
  }
  .alph-list{
    overflow: hidden;
    padding:0.2rem;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
  }
  .alph-list  .alph-wrapper{
    width: 16.6%;
    height: 0.8rem;
    line-height: 0.8rem;
    text-align: center;
  }
  .item-list{
    overflow: hidden;
    padding:0.2rem;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
  }
  .item-list .item{
    width: 25%;
    height: 0.85rem;
    line-height: 0.85rem;
    text-align: center;

  }
</style>
